package com.ctlele.team.space.activity.impl;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.ctlele.team.space.R;
import com.ctlele.team.space.activity.BaseActivity;
import com.ctlele.team.space.api.Api;
import com.ctlele.team.space.api.ApiStatusCode;
import com.ctlele.team.space.api.Apis;
import com.ctlele.team.space.entity.UserInfo;
import com.ctlele.team.space.entity.vo.resp.UserInfoResp;
import com.ctlele.team.space.util.SharedPreferencesUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.ctlele.team.space.widget.toast.IToast;
import okhttp3.Call;
import okhttp3.Response;

public class LoginActivity extends BaseActivity {

    private static final String TAG = "LoginActivity";

    public static final int REQUEST_CODE = 654961;

    public static final String CONFIRM_LOGIN_KEY = "confirm_login";

    private static final String APP_NAME = "com.ctlele.team.space";

    private static final String RECORD_USER_INFO = "login_info";

    public boolean mIsLogin = false;

    private TextView mUsername;

    private TextView mPassword;

    private Button mEnterLogin;

    private ImageView mGoBack;

    @Override
    protected int initLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        mUsername = findViewById(R.id.edit_username);
        mPassword = findViewById(R.id.edit_password);
        mEnterLogin = findViewById(R.id.login_enter_btn);
        mGoBack = findViewById(R.id.go_back);
    }

    @Override
    protected void initData() {
        if (!SharedPreferencesUtils.isEmpty(getContext(), APP_NAME, RECORD_USER_INFO)) {
            String userInfo = SharedPreferencesUtils.get(getContext(), APP_NAME, RECORD_USER_INFO);
            UserInfo info = JSON.parseObject(userInfo, UserInfo.class);
            mUsername.setText(info.getUsername());
            mPassword.setText(info.getPassword());
        }

        mEnterLogin.setOnClickListener((view) -> {
            List<Api.RequestParam> params = new ArrayList<>();
            params.add(new Api.RequestParam("username", mUsername.getText().toString().trim()));
            params.add(new Api.RequestParam("password", mPassword.getText().toString().trim()));
            Api.postForm(getContext(), Apis.LOGIN, params, new Api.ApiCallback() {
                @Override
                public void onFailure(Context context, Call call, IOException e) {
                    Log.e(TAG, e.getMessage(), e);
                }

                @Override
                public void onResponse(Context context, Call call, Response response) throws IOException {
                    String res = response.body().string();
                    UserInfoResp resp = JSON.parseObject(res, UserInfoResp.class);
                    if (resp != null && resp.getCode().equals(ApiStatusCode.OK)) {
                        runOnUiThread(() -> {
                            mIsLogin = true;
                            UserInfo info = new UserInfo();
                            info.setUsername(mUsername.getText().toString().trim());
                            info.setPassword(mPassword.getText().toString().trim());
                            String json = JSON.toJSONString(info);
                            SharedPreferencesUtils.set(getContext(), APP_NAME, RECORD_USER_INFO, json);
                            IToast.success(getContext(), "登录成功").andOnUiThread(() -> onBackPressed());
                        });
                    } else {
                        IToast.fail(getContext(), "用户名或密码错误").andOnUiThread(() -> mIsLogin = false);
                    }
                }
            });
        });

        mGoBack.setOnClickListener((view) -> {
            onBackPressed();
        });
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra(CONFIRM_LOGIN_KEY, mIsLogin);
        setResult(RESULT_OK, intent);
        super.onBackPressed();
    }
}